import sys

'''
exports cdf data to table format plottable using pgfplotter
'''
def ExportCDF(filename, lftoe_bin, lftoe_cdf, hftoe_bin, hftoe_cdf, static_bin, static_cdf, optimal_bin, optimal_cdf):
	curr_line = "lftoe_bin lftoe_cdf hftoe_bin hftoe_cdf static_bin static_cdf optimal_bin optimal_cdf\n"
	assert(len(lftoe_cdf) == len(lftoe_bin))
	assert(len(hftoe_cdf) == len(hftoe_bin))
	assert(len(static_cdf) == len(static_bin))
	num_entries = len(static_cdf)
	for offset in range(num_entries):
		curr_line += ("" + str(lftoe_bin[offset]) + " ")
		curr_line += ("" + str(lftoe_cdf[offset]) + " ")

		curr_line += ("" + str(hftoe_bin[offset]) + " ")
		curr_line += ("" + str(hftoe_cdf[offset]) + " ")

		curr_line += ("" + str(static_bin[offset]) + " ")
		curr_line += ("" + str(static_cdf[offset]) + " ")

		curr_line += ("" + str(optimal_bin[offset]) + " ")
		curr_line += ("" + str(optimal_cdf[offset]) + "\n")
	with open(filename, "w+") as f:
		f.write(curr_line)
	return

'''
exports coordinates of each traffic point's projection along the 3 cluster centroids
'''
def ExportTraffic3DProjection(filename, projections):
	for entry in projections:
		assert(len(entry) == 3)
	curr_line = ""
	num_entries = len(projections)
	for offset in range(num_entries):
		curr_line += ("" + str(projections[offset][0]) + " ")
		curr_line += ("" + str(projections[offset][1]) + " ")
		curr_line += ("" + str(projections[offset][2]) + " \n")
	with open(filename, "w+") as f:
		f.write(curr_line)
	return

'''
exports coordinates of each traffic point's projection along the 3 cluster centroids
'''
def ExportTimeseriesPerformance(filename, timeseries, lftoe_perf, hftoe_perf, static_perf, optimal_perf, cluster_indices):
	assert(len(lftoe_perf) == len(timeseries) and len(hftoe_perf) == len(lftoe_perf) and len(static_perf) == len(lftoe_perf))
	curr_line = "timeseries lftoe_perf hftoe_perf static_perf optimal_perf cluster_indices\n"
	num_entries = len(timeseries)
	for offset in range(num_entries):
		curr_line += ("" + str(timeseries[offset]) + " ")
		curr_line += ("" + str(lftoe_perf[offset]) + " ")
		curr_line += ("" + str(hftoe_perf[offset]) + " ")
		curr_line += ("" + str(static_perf[offset]) + " ")
		curr_line += ("" + str(optimal_perf[offset]) + " ")
		curr_line += ("" + str(cluster_indices[offset]) + " \n")
	with open(filename, "w+") as f:
		f.write(curr_line)
	return

def ExportDiffReconfigSimulation(filename, mttoe_mlus_diff_reconfig_freq, 
								pttoe_ave_mlus_diff_reconfig_freq, 
								pttoe_max_mlus_diff_reconfig_freq, 
								mttoe_stretch_diff_reconfig_freq, 
								pttoe_ave_stretch_diff_reconfig_freq,
								pttoe_max_stretch_diff_reconfig_freq,
								mttoe_alus_diff_reconfig_freq, 
								pttoe_ave_alus_diff_reconfig_freq,
								pttoe_max_alus_diff_reconfig_freq):
	offset = 0
	str_builder = ""
	reconfig_freqs = sorted(mttoe_mlus_diff_reconfig_freq.keys())
	for reconfig_freq in reconfig_freqs:
		str_builder += ("mlu_mttoe_" + str(reconfig_freq) + " " + "stretch_mttoe_" + str(reconfig_freq) + " " + "alu_mttoe_" + str(reconfig_freq) + " " + "mlu_pttoe_ave_" + str(reconfig_freq) + " " + "stretch_pttoe_ave_" + str(reconfig_freq) + " " + "alu_pttoe_ave_" + str(reconfig_freq) + " " + "mlu_pttoe_max_" + str(reconfig_freq) + " " + "stretch_pttoe_max_" + str(reconfig_freq) + " " + "alu_pttoe_max_" + str(reconfig_freq) + " ")
	str_builder += "\n"
	lines_to_write = len(mttoe_mlus_diff_reconfig_freq[reconfig_freqs[0]])
	for offset in range(lines_to_write):
		for reconfig_freq in reconfig_freqs:
			str_builder += ("{0:.5f}".format(mttoe_mlus_diff_reconfig_freq[reconfig_freq][offset]) + " " + "{0:.5f}".format(mttoe_stretch_diff_reconfig_freq[reconfig_freq][offset]) + " " + "{0:.9f}".format(mttoe_alus_diff_reconfig_freq[reconfig_freq][offset]) + " ")
			str_builder += ("{0:.5f}".format(pttoe_ave_mlus_diff_reconfig_freq[reconfig_freq][offset]) + " " + "{0:.5f}".format(pttoe_ave_stretch_diff_reconfig_freq[reconfig_freq][offset]) + " " + "{0:.9f}".format(pttoe_ave_alus_diff_reconfig_freq[reconfig_freq][offset]) + " ")
			str_builder += ("{0:.5f}".format(pttoe_max_mlus_diff_reconfig_freq[reconfig_freq][offset]) + " " + "{0:.5f}".format(pttoe_max_stretch_diff_reconfig_freq[reconfig_freq][offset]) + " " + "{0:.9f}".format(pttoe_max_alus_diff_reconfig_freq[reconfig_freq][offset]) + " ")
		str_builder += "\n"
		offset += 1
	with open(filename, "w+") as f:
		f.write(str_builder)
	return

def export_timeseries_performance(filename, timestamps, perf_tuple):
	num_timestamps = len(timestamps)
	assert(len(timestamps) == len(perf_tuple))
	str_builder = "timestamp mlu lu90 lu50 avehopcount \n"
	for ts in range(num_timestamps):
		perf_tup = perf_tuple[ts]
		line = "{} {} {} {} {} \n".format(timestamps[ts], perf_tup[0], perf_tup[1], perf_tup[2], perf_tup[3])
		str_builder += line
	with open(filename, "w+") as f:
		f.write(str_builder)
	return

def export_sorted_timeseries_performance(filename,dsf ):
	return "dsf"
